//
//  LocalSubstitutionCache.m
//  LocalSubstitutionCache
//
//  Created by Matt Gallagher on 2010/09/06.
//  Copyright 2010 Matt Gallagher. All rights reserved.
//
//  Permission is given to use this source code file, free of charge, in any
//  project, commercial or otherwise, entirely at your risk, with the condition
//  that any redistribution (in part or whole) of source code must retain
//  this copyright and permission notice. Attribution in compiled projects is
//  appreciated but not required.
//

#import "LocalSubstitutionCache.h"

@implementation LocalSubstitutionCache

- (NSDictionary *)substitutionPaths
{
	return
		[NSDictionary dictionaryWithObjectsAndKeys:
            @"icon2.png", @"http://good.ci123.com/app2/baobao/android/images/icon2.gif",
            @"icon3.png", @"http://good.ci123.com/app2/baobao/android/images/icon3.gif",
            @"icon4.png", @"http://good.ci123.com/app2/baobao/android/images/icon4.gif",
            @"icon5.png", @"http://good.ci123.com/app2/baobao/android/images/icon5.gif",
            @"icon6.png", @"http://good.ci123.com/app2/baobao/android/images/icon6.gif",
            @"icon9.png", @"http://good.ci123.com/app2/baobao/android/images/icon9.gif",
            @"icon10.png", @"http://good.ci123.com/app2/baobao/android/images/icon10.gif",
            @"back2.png", @"http://baby.ci123.com/yunqi/images/m/back2.png",
            @"mlogo.gif", @"http://baby.ci123.com/yunqi/images/mlogo.gif",  
            @"e102.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e102.gif",
            @"e112.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e112.gif",
            @"e113.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e113.gif",
            @"e120.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e120.gif",
            @"e121.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e121.gif",
            @"e127.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e127.gif",
            @"e140.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e140.gif",
            @"e163.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e163.gif",
            @"e178.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e178.gif",
            @"e179.gif", @"http://ctc.qzonestyle.gtimg.cn/qzone/em/e179.gif",
            @"remind.png", @"http://baby.ci123.com/yunqi/images/m/remind.png",
            @"qq2.png", @"http://baby.ci123.com/yunqi/images/m/qq2.png",
            @"re.png", @"http://baby.ci123.com/yunqi/images/m/re.png",
            @"avatar2.png", @"http://baby.ci123.com/yunqi/images/m/avatar2.png",
            @"headerbg2.png", @"http://baby.ci123.com/yunqi/ios/images/headerbg2.png",
            @"re.png", @"http://baby.ci123.com/yunqi/images/m/re.png",
            @"j.png", @"http://baby.ci123.com/yunqi/images/m/j.png",
            @"tree1.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree1.png",
            @"tree2.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree2.png",
            @"tree3.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree3.png",
            @"tree4.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree4.png",
            @"tree5.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree5.png",
            @"tree6.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree6.png",
            @"tree7.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree7.png",
            @"tree8.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree8.png",
            @"tree9.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree9.png",
            @"tree10.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree10.png",
            @"tree11.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree11.png",
            @"tree12.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree12.png",
            @"tree13.png", @"http://blogstatic.ci123.com/treestatic/images/img/tree13.png",
            @"sun.png", @"http://baby.ci123.com/yunqi/ios/images/1.png",
            @"loading_32.gif", @"http://file2.ci123.com/ast/images/loading_32.gif",
            @"a3.png", @"http://baby.ci123.com/yunqi/images/m/a3.png",
            @"set.png", @"http://baby.ci123.com/yunqi/images/m/set.png",
		nil];
}

- (NSString *)mimeTypeForPath:(NSString *)originalPath
{
	//
	// Current code only substitutes PNG images
	//
	return @"image/png";	
}

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
	//
	// Get the path for the request
	//
	NSString *pathString = [[request URL] absoluteString];
	
	//
	// See if we have a substitution file for this path
	//
	NSString *substitutionFileName = [[self substitutionPaths] objectForKey:pathString];
	if (!substitutionFileName)
	{
		//
		// No substitution file, return the default cache response
		//
		return [super cachedResponseForRequest:request];
	}
	
	//
	// If we've already created a cache entry for this path, then return it.
	//
	NSCachedURLResponse *cachedResponse = [cachedResponses objectForKey:pathString];
	if (cachedResponse)
	{
		return cachedResponse;
	}
	
	//
	// Get the path to the substitution file
	//
	NSString *substitutionFilePath =
		[[NSBundle mainBundle]
			pathForResource:[substitutionFileName stringByDeletingPathExtension]
			ofType:[substitutionFileName pathExtension]];
	NSAssert(substitutionFilePath, @"File %@ in substitutionPaths didn't exist", substitutionFileName);
	
	//
	// Load the data
	//
	NSData *data = [NSData dataWithContentsOfFile:substitutionFilePath];
	
	//
	// Create the cacheable response
	//
	NSURLResponse *response =
		[[[NSURLResponse alloc]
			initWithURL:[request URL]
			MIMEType:[self mimeTypeForPath:pathString]
			expectedContentLength:[data length]
			textEncodingName:nil]
		autorelease];
	cachedResponse =
		[[[NSCachedURLResponse alloc] initWithResponse:response data:data] autorelease];
	
	//
	// Add it to our cache dictionary
	//
	if (!cachedResponses)
	{
		cachedResponses = [[NSMutableDictionary alloc] init];
	}
	[cachedResponses setObject:cachedResponse forKey:pathString];
	
	return cachedResponse;
}

- (void)removeCachedResponseForRequest:(NSURLRequest *)request
{
	//
	// Get the path for the request
	//
	NSString *pathString = [[request URL] path];
	if ([cachedResponses objectForKey:pathString])
	{
		[cachedResponses removeObjectForKey:pathString];
	}
	else
	{
		[super removeCachedResponseForRequest:request];
	}
}

- (void)dealloc
{
	[cachedResponses release];
	cachedResponses = nil;
	[super dealloc];
}

@end
